// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_procedure_details_ok.java

package manufacture.procedure;

import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class register_procedure_details_ok extends HttpServlet
{

	public register_procedure_details_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String manufacture_ID = request.getParameter("manufacture_ID");
				String labour_hour_amount = request.getParameter("labour_hour_amount");
				String product_name[] = request.getParameterValues("product_name");
				String amount[] = request.getParameterValues("amount");
				int module_amount = 0;
				if (product_name != null)
					module_amount = product_name.length;
				int p = 0;
				for (int i = 0; i < module_amount; i++)
					if (!amount[i].equals("") && !validata.validata(amount[i]))
						p++;

				if (!labour_hour_amount.equals("") && !validata.validata(labour_hour_amount))
					p++;
				if (p == 0)
				{
					String procedure_name = request.getParameter("procedure_name");
					session.setAttribute("procedure_name", procedure_name);
					String procedure_responsible_person = request.getParameter("procedure_responsible_person");
					String register = request.getParameter("register");
					String register_time = request.getParameter("register_time");
					String real_labour_hour_amount = request.getParameter("real_labour_hour_amount");
					String details_number1 = request.getParameter("details_number1");
					String real_amount[] = request.getParameterValues("real_module_amount");
					String extra_amount[] = request.getParameterValues("extra_amount");
					String design_amount[] = request.getParameterValues("design_amount");
					String details_number[] = request.getParameterValues("details_number");
					String product_ID[] = request.getParameterValues("product_ID");
					String product_describe[] = request.getParameterValues("product_describe");
					String type[] = request.getParameterValues("type");
					String amount_unit[] = request.getParameterValues("amount_unit");
					String cost_price[] = request.getParameterValues("cost_price");
					if (product_name != null)
					{
						int n = 0;
						for (int i = 0; i < product_name.length; i++)
							if (!amount[i].equals("") && Double.parseDouble(real_amount[i]) + Double.parseDouble(amount[i]) > Double.parseDouble(design_amount[i]) + Double.parseDouble(extra_amount[i]))
								n++;

						if (n == 0)
						{
							if (labour_hour_amount.equals(""))
								labour_hour_amount = "0";
							try
							{
								String sql8 = (new StringBuilder("select * from manufacture_procedure where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("'").toString();
								ResultSet rs8 = manufacture_db.executeQuery(sql8);
								if (rs8.next())
								{
									String sql = (new StringBuilder("insert into manufacture_proceduring(manufacture_ID,procedure_name,procedure_ID,details_number,procedure_describe,amount_unit,cost_price) values('")).append(rs8.getString("manufacture_ID")).append("','").append(rs8.getString("procedure_name")).append("','").append(rs8.getString("procedure_ID")).append("','").append(rs8.getString("details_number")).append("','").append(rs8.getString("procedure_describe")).append("','").append(rs8.getString("amount_unit")).append("','").append(rs8.getString("cost_price")).append("')").toString();
									manufacture_db.executeUpdate(sql);
								}
								double subtotal = 0.0D;
								double labour_cost_price_sum = 0.0D;
								String sql1 = (new StringBuilder("select * from manufacture_proceduring where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("' order by id desc").toString();
								ResultSet rs1 = manufacture_db.executeQuery(sql1);
								if (rs1.next())
								{
									subtotal = rs1.getDouble("cost_price") * Double.parseDouble(labour_hour_amount);
									double amount1 = Double.parseDouble(labour_hour_amount) + Double.parseDouble(real_labour_hour_amount);
									double subtotal1 = rs1.getDouble("cost_price") * amount1;
									String sql2 = (new StringBuilder("update manufacture_proceduring set labour_hour_amount='")).append(labour_hour_amount).append("',subtotal='").append(subtotal).append("',register='").append(register).append("',register_time='").append(register_time).append("' where id='").append(rs1.getString("id")).append("'").toString();
									manufacture_db.executeUpdate(sql2);
									String sql3 = (new StringBuilder("update manufacture_procedure set procedure_check_tag='1',procedure_finish_tag='1' where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("'").toString();
									manufacture_db.executeUpdate(sql3);
								}
								String sql9 = (new StringBuilder("select * from manufacture_manufacture where manufacture_ID='")).append(manufacture_ID).append("'").toString();
								ResultSet rs9 = manufacture_db.executeQuery(sql9);
								if (rs9.next())
								{
									String sql10 = (new StringBuilder("update manufacture_manufacture set manufacture_procedure_tag='1',manufacture_procedure_check_tag='1' where manufacture_ID='")).append(manufacture_ID).append("'").toString();
									manufacture_db.executeUpdate(sql10);
								}
								double module_cost_price_sum = 0.0D;
								for (int i = 0; i < product_name.length; i++)
									if (!amount[i].equals(""))
									{
										double module_subtotal = Double.parseDouble(cost_price[i]) * Double.parseDouble(amount[i]);
										double amount2 = Double.parseDouble(real_amount[i]) + Double.parseDouble(amount[i]);
										double subtotal2 = Double.parseDouble(cost_price[i]) * amount2;
										module_cost_price_sum += subtotal2;
										String sql4 = (new StringBuilder("insert into manufacture_procedure_moduling(manufacture_ID,procedure_name,details_number,product_ID,product_name,product_describe,type,amount,amount_unit,cost_price,subtotal,register,register_time) values('")).append(manufacture_ID).append("','").append(procedure_name).append("','").append(details_number[i]).append("','").append(product_ID[i]).append("','").append(product_name[i]).append("','").append(product_describe[i]).append("','").append(type[i]).append("','").append(amount[i]).append("','").append(amount_unit[i]).append("','").append(cost_price[i]).append("','").append(module_subtotal).append("','").append(register).append("','").append(register_time).append("')").toString();
										manufacture_db.executeUpdate(sql4);
									} else
									{
										double subtotal3 = Double.parseDouble(cost_price[i]) * Double.parseDouble(real_amount[i]);
										module_cost_price_sum += subtotal3;
									}

							}
							catch (Exception ex)
							{
								ex.printStackTrace();
							}
							response.sendRedirect((new StringBuilder("manufacture/procedure/register_procedure_details_ok_a.jsp?manufacture_ID=")).append(manufacture_ID).append("&&procedure_name=").append(procedure_name).toString());
						} else
						{
							response.sendRedirect((new StringBuilder("manufacture/procedure/register_procedure_details_ok_b.jsp?manufacture_ID=")).append(manufacture_ID).append("&&procedure_name=").append(procedure_name).toString());
						}
					} else
					{
						if (labour_hour_amount.equals(""))
							labour_hour_amount = "0";
						try
						{
							String sql88 = (new StringBuilder("select * from manufacture_procedure where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("'").toString();
							ResultSet rs88 = manufacture_db.executeQuery(sql88);
							if (rs88.next())
							{
								String sql = (new StringBuilder("insert into manufacture_proceduring(manufacture_ID,procedure_name,procedure_ID,details_number,procedure_describe,amount_unit,cost_price) values('")).append(rs88.getString("manufacture_ID")).append("','").append(rs88.getString("procedure_name")).append("','").append(rs88.getString("procedure_ID")).append("','").append(rs88.getString("details_number")).append("','").append(rs88.getString("procedure_describe")).append("','").append(rs88.getString("amount_unit")).append("','").append(rs88.getString("cost_price")).append("')").toString();
								manufacture_db.executeUpdate(sql);
							}
							double subtotal = 0.0D;
							double labour_cost_price_sum = 0.0D;
							String sql1 = (new StringBuilder("select * from manufacture_proceduring where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("' order by id desc").toString();
							ResultSet rs1 = manufacture_db.executeQuery(sql1);
							if (rs1.next())
							{
								subtotal = rs1.getDouble("cost_price") * Double.parseDouble(labour_hour_amount);
								double amount1 = Double.parseDouble(labour_hour_amount) + Double.parseDouble(real_labour_hour_amount);
								double subtotal1 = rs1.getDouble("cost_price") * amount1;
								String sql2 = (new StringBuilder("update manufacture_proceduring set labour_hour_amount='")).append(labour_hour_amount).append("',subtotal='").append(subtotal).append("',register='").append(register).append("',register_time='").append(register_time).append("' where id='").append(rs1.getString("id")).append("'").toString();
								manufacture_db.executeUpdate(sql2);
								String sql3 = (new StringBuilder("update manufacture_procedure set procedure_check_tag='1',procedure_finish_tag='1' where manufacture_ID='")).append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and details_number='").append(details_number1).append("'").toString();
								manufacture_db.executeUpdate(sql3);
							}
							String sql9 = (new StringBuilder("select * from manufacture_manufacture where manufacture_ID='")).append(manufacture_ID).append("'").toString();
							ResultSet rs9 = manufacture_db.executeQuery(sql9);
							if (rs9.next())
							{
								String sql10 = (new StringBuilder("update manufacture_manufacture set manufacture_procedure_tag='1',manufacture_procedure_check_tag='1' where manufacture_ID='")).append(manufacture_ID).append("'").toString();
								manufacture_db.executeUpdate(sql10);
							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect((new StringBuilder("manufacture/procedure/register_procedure_details_ok_c.jsp?manufacture_ID=")).append(manufacture_ID).append("&&procedure_name=").append(procedure_name).toString());
					}
				} else
				{
					response.sendRedirect((new StringBuilder("manufacture/procedure/register_procedure_details_ok_d.jsp?manufacture_ID=")).append(manufacture_ID).toString());
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
